package org.xiaom.butler.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xiaom.butler.R;

import android.content.Context;

public class NetHelper {
	
	public static String getRootUrl(Context context) {
		return context.getString(R.string.server_protocol) + "://"
				+ context.getString(R.string.server_name) + ":"
				+ context.getString(R.string.server_port) + "/"
				+ context.getString(R.string.server_root);
	}

	public String getString(String url) {
		StringBuffer result = new StringBuffer();
		String rs = "";
		HttpGet get = new HttpGet(url);
		HttpClient httpClient = new DefaultHttpClient();
		try {
			HttpResponse response = httpClient.execute(get);
			HttpEntity entity = response.getEntity();
			BufferedReader br = new BufferedReader(new InputStreamReader(
					entity.getContent()));
			String ts = "";
			while ((ts = br.readLine()) != null) {
				rs += ts;
			}
			result.append(rs);
		} catch (Exception e) {
			e.printStackTrace();
			result.append("");
		} finally {

		}
		return result.toString();
	}

	public InputStream getInputStream(String url) {
		HttpClient hc = new DefaultHttpClient();
		HttpGet get = new HttpGet(url);
		InputStream in = null;
		try {
			HttpResponse hr = hc.execute(get);
			HttpEntity he = hr.getEntity();
			in = he.getContent();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return in;
	} 
}
